package com.qr.routing;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer_Routing2 {
    public static void main(String[] args) throws Exception {
        //        创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
//        设置参数
        connectionFactory.setHost("192.168.150.100");
        connectionFactory.setPort(5672);
        //设置虚拟机，有一个默认值/
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

//        创建连接connection
        Connection connection = connectionFactory.newConnection();
//        创建channel
        Channel channel = connection.createChannel();

        String queueName1 = "direct_queue1";
        String queueName2 = "direct_queue2";

        //接收消息
        /**
         * queuq:队列名称
         * autoAck：是否自动确认，消费者一旦收到消息，自动给mq回复
         * callback:回调对象
         */
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){
            /**
             * 回调方法，当收到消息会自动执行该方法
             * consumerTag：消息标识
             * envelope：获取一些信息，交换机，路由key
             * properties:配置信息
             * body：数据
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                //队列一打印信息
                System.out.println(new String(body));
                System.out.println("队列一打印信息");
                super.handleDelivery(consumerTag, envelope, properties, body);
            }
        };
        channel.basicConsume(queueName2,true,defaultConsumer);

    }
}
